Object.defineProperty(exports, "__esModule", {
    value: !0
});

var e = function(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}(require("../../../../utils/api2"));

exports.default = {
    $$imageHideLayoutPicker: function() {
        this.setData({
            "$$editor.$$imageLayoutPicker.isShown": !1
        });
    },
    $$imageSelectLayout: function(e) {
        var a = e.currentTarget.dataset.index;
        this.setData({
            "$$editor.$$imageLayoutPicker.activeLayout": a
        });
    },
    $$chooseImageSource: function(e) {
        var a = this;
        e.currentTarget.dataset.sid ? wx.showActionSheet({
            itemList: [ "拍照", "从手机相册选择", "从素材库选择" ],
            success: function(e) {
                var t = e.tapIndex;
                if (a.setData({
                    "$$editor.$$imageLayoutPicker.isShown": !1
                }), 0 === t || 1 === t) a.$$chooseImageFromLocal(0 == t ? "camera" : "album"); else {
                    a.$addListener("material:image:choose", function(e) {
                        a.$removeAllListeners("material:image:choose");
                        var t = e.map(function(e) {
                            return {
                                desc: "",
                                link: null,
                                url: e.source,
                                fieldid: e.aid
                            };
                        });
                        a.$$handleChooseImages(t);
                    });
                    var i = a.data.sidebar.site.sid, o = a.data.$$editor.$$imageLayoutPicker, r = o.activeLayout, n = o.layoutList[r].model;
                    a.$route("/pages/editor/material/image/image?sid=" + i + "&model=" + n);
                }
            }
        }) : console.error("no sid");
    },
    $$handleChooseImages: function(e) {
        var a = this.data.sidebar.site.sid, t = this.data.$$editor.$$imageLayoutPicker, i = t.activeLayout, o = t.layoutList, r = t.key, n = o[i].model;
        "paved" !== n && (e = e.slice(0, "splicing" === n ? 5 : 9), this.$$editorAddSection({
            type: "image",
            data: {
                images: e,
                modid: 1,
                current: 0
            }
        }, r)), this.$$imageManagerData = e, wx.redirectTo({
            url: "/pages/editor/image-manager/image-manager?sid=" + a + "&model=" + n + "&modid=1&key=" + r
        });
    },
    $$chooseImageFromLocal: function() {
        var e = this, a = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : "camera", t = this.data.sidebar.site.sid, i = this.data.$$editor.$$imageLayoutPicker, o = i.activeLayout, r = i.layoutList, n = i.key, s = r[o].model;
        wx.chooseImage({
            count: "splicing" === s ? 5 : 9,
            sizeType: [ "compressed" ],
            sourceType: [ a ],
            success: function(a) {
                var i = e._filterImages(a.tempFiles);
                i.length ? e.uploadImage(i).then(function(a) {
                    wx.hideLoading(), "paved" !== s && e.$$editorAddSection({
                        type: "image",
                        data: {
                            images: a,
                            model: s,
                            modid: 1,
                            current: 0
                        }
                    }, n), e.$$imageManagerData = a, e.$route("/pages/editor/image-manager/image-manager?sid=" + t + "&model=" + s + "&modid=1&key=" + n);
                }) : wx.hideLoading();
            }
        });
    },
    uploadImage: function() {
        var a = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [];
        return a.length ? (wx.showLoading({
            title: "上传图片中..."
        }), Promise.all(a.map(function(a) {
            return e.default.site.attachment.upload({
                data: {
                    file: a,
                    recordid: ""
                },
                upload: "file"
            });
        })).then(function(e) {
            var a = [];
            return e.forEach(function(e) {
                var t = e.fieldid, i = e.url;
                a.push({
                    fieldid: t,
                    url: i,
                    desc: "",
                    link: null
                });
            }), e.length !== a.length ? new Promise(function(t) {
                wx.showModal({
                    content: e.length - a.length + "上传失败",
                    showCancel: !1,
                    success: function() {
                        t(a);
                    }
                });
            }) : a;
        })) : Promise.reject("文件路径错误");
    },
    _filterImages: function(e) {
        var a = [], t = 0;
        return e.forEach(function(e) {
            e.size > 2e6 ? t += 1 : a.push(e.path);
        }), t && wx.showModal({
            title: "上传提示",
            content: "部分上传图片大于2M, 请压缩后上传",
            showCancel: !1,
            confirmText: "知道了"
        }), a;
    }
};